package string.serveur;

import java.awt.List;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

public class ServeurString 
{
	
	private ArrayList<Personne> listeNom;
	private DataOutputStream emission;
	private BufferedReader reception;
	private ServerSocket socket;
	private Socket s;
	
	public ServeurString()
	{
		listeNom = new ArrayList<Personne>();
		try 
		{
			System.out.println("Demarage du serveeeur");
			socket = new ServerSocket(6666);
			System.out.println("Serveur démarré");
		} 
		catch (IOException e) 
		{
			System.out.println("Erreur lors du demarrage du serveur");
		}
		connexion();
	}
	
	public String[] tokenize(String s)
	{
		String[] retour = s.split("#");
		return retour;
	}
	
	public void connexion()
	{	
		try 
		{	
				s = this.getSocket().accept();
				this.setReception(s.getInputStream());
				this.setEmission(s.getOutputStream());
		} 
		catch (IOException e) 
		{
			System.err.println("Plantage !");
		}
		
		communication();
	}
	
	public void communication()
	{
		String[] tabS;
		String chaine = new String();
		while(!s.isClosed())
		{
			try 
			{
				chaine = this.getReception().readLine();
			} 
			catch (IOException e)
			{
				System.out.println("Erreur de lecture sur le reader");
				connexion();
			}
			tabS = tokenize(chaine);
			traiter(tabS);
		}
		System.out.println("Communication interrompue par le client");
		connexion();
	}
	
	public void traiter(String[] s)
	{
		Methode m = Methode.valueOf(s[0]);
		switch (m) 
		{
			case ADD:
				add(s);
			break;
			
			case GETNICKNAME:
				nickname(s);
			break;
				
			case GETNAME:
				name(s);
			break;
			
			case SET:
				set(s);
			break;
			
			case DELNAME:
				//delname(s);
			break;
			
			case DELNICKNAME:
				//delnickname(s);
			break;
		}
	}

	private void set(String[] s) 
	{
		if(s.length == 2)
		{
			for(Personne p : getListeNom())
			{
				for(int i = 0; i < p.getNickname().size();i++)
				{
					if(p.getNickname().get(i) == s[1])
					{
						p.getNickname().add(s[2]);
						p.getNickname().remove(i);
						StringBuilder sb = new StringBuilder();
						sb.append("0");
						sb.append("#");
						sb.append("\n");
						try 
						{
							getEmission().writeBytes(sb.toString());
							return;
						}
						catch (IOException e) 
						{
							System.out.println("Erreur dans l'envoi de retour");
						}
					}
				}
			}
			StringBuilder sb = new StringBuilder();
			sb.append("3");
			sb.append("\n");
			try 
			{
				getEmission().writeBytes(sb.toString());
				return;
			} 
			catch (IOException e) 
			{
				System.out.println("Erreur dans l'envoi de retour");
			}
		}
	}

	private void name(String[] s) 
	{
		if(s.length == 2)
		{
			for(Personne p : getListeNom())
			{
				for(int i = 0; i < p.getNickname().size();i++)
				{
					if(p.getNickname().get(i) == s[1])
					{
						StringBuilder sb = new StringBuilder();
						sb.append("0");
						sb.append("#" + p.getName());
						sb.append("\n");
						try 
						{
							getEmission().writeBytes(sb.toString());
							return;
						}
						catch (IOException e) 
						{
							System.out.println("Erreur dans l'envoi de retour");
						}
					}
				}
			}
			StringBuilder sb = new StringBuilder();
			sb.append("3");
			sb.append("\n");
			try 
			{
				getEmission().writeBytes(sb.toString());
				return;
			} 
			catch (IOException e) 
			{
				System.out.println("Erreur dans l'envoi de retour");
			}
		}
	}

	public void add(String[] s)
	{
		if(s.length == 3)
		{
			for(Personne p : getListeNom())
			{
				if(p.getName() == s[1])
				{
					StringBuilder sb = new StringBuilder();
					sb.append("2");
					sb.append("\n");
					try 
					{
						getEmission().writeBytes(sb.toString());
						return;
					} 
					catch (IOException e) 
					{
						System.out.println("Erreur dans l'envoi de retour");
					}
				}
			}
			Personne p = new Personne(s[1], s[2]);
			this.getListeNom().add(p);
		}
	}

	private void nickname(String[] s) 
	{
		if(s.length == 2)
		{
			for(Personne p : getListeNom())
			{
				if(p.getName().equalsIgnoreCase(s[1]))
				{
					StringBuilder sb = new StringBuilder();
					sb.append("0");
					for(int i = 0; i < p.getNickname().size();i++)
					{
						sb.append("#"+p.getNickname().get(i));
					}
					sb.append("\n");
					try 
					{
						getEmission().writeBytes(sb.toString());
						return;
					} 
					catch (IOException e) 
					{
						System.out.println("Erreur dans l'envoi de retour");
					}
				}
			}
			StringBuilder sb = new StringBuilder();
			sb.append("4");
			sb.append("\n");
			try 
			{
				getEmission().writeBytes(sb.toString());
				return;
			} 
			catch (IOException e) 
			{
				System.out.println("Erreur dans l'envoi de retour");
			}
		}
	}
	
	public DataOutputStream getEmission() {
		return emission;
	}

	public void setEmission(OutputStream emission) {
		this.emission = new DataOutputStream(emission);
	}

	public BufferedReader getReception() {
		return reception;
	}

	public void setReception(InputStream reception) {
		this.reception = new BufferedReader(new InputStreamReader(reception));
	}

	public ServerSocket getSocket() {
		return socket;
	}
	
	public ArrayList<Personne> getListeNom()
	{
		return this.listeNom;
	}
	
	public enum Methode
	{
		ADD("add"),
		GETNICKNAME("get_nickname"),
		GETNAME("get_name"),
		SET("set"),
		DELNAME("del_name"),
		DELNICKNAME("del_nickname");
		
		private String chaine;
		private Methode(String s){chaine = s;}
	}
	
	public static void main(String[] args) 
	{
		ServeurString serv = new ServeurString();
	}
	
}